/*
  学习目标：分离reducer和初始值
  1. 将reducer提取为单独的函数
  2. 使用switch，替换if语句
  3. 提取变量，作为初始状态
*/

import { createStore } from 'redux';

// 3. 提取一个变量作为初始state
const initState = {
    count: 0,
    msg: 'Hello React',
};

// 1. 声明reducer
function reducer(state = initState, action) {
    console.warn('进到仓库了, action是  ----->', action);
    // 2. 使用switch与替换if语句
    switch (action.type) {
        case 'add':
            return { ...state, count: state.count + 1 };

        case 'des':
            return { ...state, count: state.count - 1 };

        default:
            return state;
    }
}

const store = createStore(reducer);

const unSubFn = store.subscribe(() => {
    console.log('状态发生变化了, state的值  ----->  ', store.getState());
});

store.dispatch({ type: 'add' });
store.dispatch({ type: 'des' });
unSubFn();
store.dispatch({ type: 'des' });
store.dispatch({ type: 'des' });

export default function App() {
    return <div>我是APP</div>;
}